﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Tailor_Measurement : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loaddata();
        }
    }

    private void loaddata()
    {
        string sql = @"

SELECT [ProductID] as ID
      ,[ProductName] as Name
FROM [Tailor_Product]

SELECT [DesignID] as ID
      ,[DesignName] as Name   
  FROM Tailor_Design

SELECT [MasterkolID] as ID
      ,[MasterkolName] as Name   
  FROM Tailor_Masterkol

SELECT [MeasurementID] as ID
      ,[MeasurementName] as Name   
  FROM Tailor_Measurement


SELECT [Design_MeasurementID] as ID
      ,[Design_MeasurementName] as Name   
  FROM Tailor_Design_Measurement
";

        DataSet ds = CommonManager.SQLExec(sql);
        ddlItem.Items.Clear();
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            ddlItem.Items.Add(new ListItem(dr["Name"].ToString(),dr["ID"].ToString()));
        }

        gvDesign.DataSource = ds.Tables[1];
        gvDesign.DataBind();

        gvMasterKol.DataSource = ds.Tables[2];
        gvMasterKol.DataBind();

        gvMeasurement.DataSource = ds.Tables[3];
        gvMeasurement.DataBind();


        gvDesign_Measurement.DataSource = ds.Tables[4];
        gvDesign_Measurement.DataBind();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string sql = "Delete Tailor_DesignProduct where ProductID="+ddlItem.SelectedValue+@";
        Delete Tailor_MasterkolProduct where ProductID=" + ddlItem.SelectedValue + @";
        Delete Tailor_MeasurementProduct where ProductID=" + ddlItem.SelectedValue + @";
        Delete Tailor_Design_MeasurementProduct where ProductID=" + ddlItem.SelectedValue + @";
        ";

        foreach (GridViewRow gvr in gvDesign.Rows)
        {
            CheckBox chkSelect = (CheckBox)gvr.FindControl("chkSelect");
            TextBox txtGroup = (TextBox)gvr.FindControl("txtGroup");
            TextBox txtOrdering = (TextBox)gvr.FindControl("txtOrdering");
            if (chkSelect.Checked)
            {
                sql += @"
                    INSERT INTO [Tailor_DesignProduct]
                           ([DesignID]
                           ,[ProductID]
                           ,[Group],[Ordering])
                     VALUES
                           (" + chkSelect.ToolTip + @"," + ddlItem.SelectedValue + @",'" + txtGroup.Text + @"'," + txtOrdering.Text + @");
                ";
            }
        }


        foreach (GridViewRow gvr in gvDesign_Measurement.Rows)
        {
            CheckBox chkSelect = (CheckBox)gvr.FindControl("chkSelect");
            TextBox txtGroup = (TextBox)gvr.FindControl("txtGroup");
            TextBox txtOrdering = (TextBox)gvr.FindControl("txtOrdering");
            if (chkSelect.Checked)
            {
                sql += @"
                    INSERT INTO [Tailor_Design_MeasurementProduct]
                           ([Design_MeasurementID]
                           ,[ProductID]
                           ,[Group],[Ordering])
                     VALUES
                           (" + chkSelect.ToolTip + @"," + ddlItem.SelectedValue + @",'" + txtGroup.Text + @"'," + txtOrdering.Text + @");
                ";
            }
        }



        foreach (GridViewRow gvr in gvMasterKol.Rows)
        {
            CheckBox chkSelect = (CheckBox)gvr.FindControl("chkSelect");
            TextBox txtOrdering = (TextBox)gvr.FindControl("txtOrdering");
            if (chkSelect.Checked)
            {
                sql += @"
                    INSERT INTO [Tailor_MasterKolProduct]
                           ([MasterKolID]
                           ,[ProductID],[Ordering])
                     VALUES
                           (" + chkSelect.ToolTip + @"," + ddlItem.SelectedValue + @"," + txtOrdering.Text + @");
                ";
            }
        }


        foreach (GridViewRow gvr in gvMeasurement.Rows)
        {
            CheckBox chkSelect = (CheckBox)gvr.FindControl("chkSelect");
            TextBox txtOrdering = (TextBox)gvr.FindControl("txtOrdering");
            TextBox txtPrintArrangement = (TextBox)gvr.FindControl("txtPrintArrangement");
            if (chkSelect.Checked)
            {
                sql += @"
                    INSERT INTO [Tailor_MeasurementProduct]
                           ([MeasurementID]
                           ,[ProductID],[Ordering],PrintArrangement)
                     VALUES
                           (" + chkSelect.ToolTip + @"," + ddlItem.SelectedValue + @"," + txtOrdering.Text + @"," + txtPrintArrangement.Text + @");
                ";
            }
        }

        CommonManager.SQLExec(sql);
    }
    protected void ddlItem_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sql = @"

SELECT [DesignID] as ID
      ,[Group] ,Ordering
  FROM [Tailor_DesignProduct]
where ProductID=" + ddlItem.SelectedValue + @" order by Ordering


SELECT [MasterKolID] as ID,Ordering
  FROM [Tailor_MasterKolProduct]
where ProductID=" + ddlItem.SelectedValue + @" order by Ordering

SELECT [MeasurementID] as ID,Ordering,PrintArrangement
  FROM [Tailor_MeasurementProduct]
where ProductID=" + ddlItem.SelectedValue + @" order by Ordering


SELECT [Design_MeasurementID] as ID,[Group] ,Ordering
  FROM [Tailor_Design_MeasurementProduct]
where ProductID=" + ddlItem.SelectedValue + @" order by Ordering

";
        DataSet ds = CommonManager.SQLExec(sql);
        foreach (GridViewRow gvr in gvDesign.Rows)
        {
            CheckBox chkSelect = (CheckBox)gvr.FindControl("chkSelect");
            TextBox txtGroup = (TextBox)gvr.FindControl("txtGroup");
            TextBox txtOrdering = (TextBox)gvr.FindControl("txtOrdering");
            chkSelect.Checked = false;

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (dr["ID"].ToString() == chkSelect.ToolTip)
                {
                    chkSelect.Checked = true;
                    txtGroup.Text = dr["Group"].ToString();
                    txtOrdering.Text = dr["Ordering"].ToString();
                }
            }
        }


        foreach (GridViewRow gvr in gvMasterKol.Rows)
        {
            CheckBox chkSelect = (CheckBox)gvr.FindControl("chkSelect");
            chkSelect.Checked = false;
            TextBox txtOrdering = (TextBox)gvr.FindControl("txtOrdering");

            foreach (DataRow dr in ds.Tables[1].Rows)
            {
                if (dr["ID"].ToString() == chkSelect.ToolTip)
                {
                    chkSelect.Checked = true;
                    txtOrdering.Text = dr["Ordering"].ToString();
                }
            }
        }


        foreach (GridViewRow gvr in gvMeasurement.Rows)
        {
            CheckBox chkSelect = (CheckBox)gvr.FindControl("chkSelect");
            chkSelect.Checked = false;
            TextBox txtOrdering = (TextBox)gvr.FindControl("txtOrdering");
            TextBox txtPrintArrangement = (TextBox)gvr.FindControl("txtPrintArrangement");

            foreach (DataRow dr in ds.Tables[2].Rows)
            {
                if (dr["ID"].ToString() == chkSelect.ToolTip)
                {
                    chkSelect.Checked = true;
                    txtOrdering.Text = dr["Ordering"].ToString();
                    txtPrintArrangement.Text = dr["PrintArrangement"].ToString();
                }
            }
        }

        foreach (GridViewRow gvr in gvDesign_Measurement.Rows)
        {
            CheckBox chkSelect = (CheckBox)gvr.FindControl("chkSelect");
            TextBox txtGroup = (TextBox)gvr.FindControl("txtGroup");
            TextBox txtOrdering = (TextBox)gvr.FindControl("txtOrdering");
            chkSelect.Checked = false;

            foreach (DataRow dr in ds.Tables[3].Rows)
            {
                if (dr["ID"].ToString() == chkSelect.ToolTip)
                {
                    chkSelect.Checked = true;
                    txtGroup.Text = dr["Group"].ToString();
                    txtOrdering.Text = dr["Ordering"].ToString();
                }
            }
        }

    }
}